//
//  WWTableCollectionCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/4.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWTableCollectionCell.h"
#import "FYPageScrollView.h"
#import "WWHeaderView.h"
#import "WWTableContentModel.h"
#import "WWContentTableCell.h"
#import "PublicHeader.h"
#import <MBProgressHUD.h>

@interface WWTableCollectionCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *headerArray;
@property (nonatomic, strong) NSMutableArray *headerTitleArray;
@property (nonatomic, strong) NSMutableArray *contentArray;
@property (nonatomic, strong) MBProgressHUD *progressView;

@end

@implementation WWTableCollectionCell

- (void)handleData:(NSMutableArray *)arr withArray:(NSMutableArray *)array {
        self.headerArray = arr;
    if (self.tableView.contentOffset.x > 10) {
        self.tableView.tableHeaderView = [[FYPageScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0) PicArray:_headerArray];
        [self.tableView reloadData];
    } else {
        self.tableView.tableHeaderView = [[FYPageScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, FitConstant_YorHeight(180)) PicArray:_headerArray];
    }
    self.headerTitleArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        [self.headerTitleArray addObject:[dic objectForKey:@"title"]];
        NSArray *temp = [NSArray arrayWithArray:[dic objectForKey:@"list"]];
        for (NSDictionary *dic in temp) {
            WWTableContentModel *model = [WWTableContentModel wwModelWithDictionary:dic];
            [self.contentArray addObject:model];
        }
    }
    [self.tableView reloadData];
    [_progressView removeFromSuperview];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.headerArray = [NSMutableArray array];
        self.contentArray = [NSMutableArray array];
        _tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWContentTableCell class] forCellReuseIdentifier:@"tableViewCell"];
        _tableView.rowHeight = 82;
        [self.contentView addSubview:_tableView];
        _progressView = [[MBProgressHUD alloc] initWithView:self.tableView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        [self.tableView addSubview:self.progressView];
        [self.tableView NightWithType:UIViewColorTypeNormal];
        [self NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.headerTitleArray.count) {
        return (self.headerTitleArray.count - 1);
    }
    return 0;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WWContentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    if (indexPath.row + indexPath.section * 3 + 1 < self.contentArray.count) {
        WWTableContentModel *model = self.contentArray[indexPath.row + indexPath.section * 3 + 1];
        cell.model = model;
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    WWHeaderView *headerView = [[WWHeaderView alloc] init];
    headerView.imageView.image = [UIImage imageNamed:@"sanjiao.png"];
    if (section < (self.headerTitleArray.count - 1)) {
        headerView.titleLabel.text = self.headerTitleArray[section + 1];
    }
    headerView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, FitConstant_YorHeight(25));
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row + indexPath.section * 3 + 1 < self.contentArray.count) {
        WWTableContentModel *model = self.contentArray[indexPath.row + indexPath.section * 3 + 1];
        NSString *aid = [NSString stringWithFormat:@"%@", model.albumId];
        NSString *uid = [NSString stringWithFormat:@"%@", model.uid];
        [self.delegate passValue:uid withAlumbTitle:model.title withAlbumId:aid];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return FitConstant_YorHeight(20);
}

@end
